package com.example.expandablelistview.cp;

import android.app.ListActivity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Context;
import android.content.Loader;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

public class ProductList_CursorLoader_Adapter extends ListActivity implements LoaderCallbacks<Cursor>{
	
	private MyCursorAdapter mAdapter;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		getLoaderManager().initLoader(0, null, this);
	}
	
	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		return new ProductCursorLoader(this);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		mAdapter = new MyCursorAdapter(this, data, true);
		setListAdapter(mAdapter);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		mAdapter.swapCursor(null);		
	}
	
	final static class ProductCursorLoader extends SimpleCursorLoader {

		private ProductDBHelper mHelper;
		private SQLiteDatabase db;
		
		public ProductCursorLoader(Context context) {
			super(context);
			mHelper = new ProductDBHelper(context);
			db = mHelper.getWritableDatabase();
		}
		@Override
		protected Cursor loadCursor() {
			return db.rawQuery("SELECT * FROM product", null);
		}
	}
}
